// pages/login/login.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    index: null,
    username: '',
    password: ''
  },

  usernameInput(e) {
    this.setData({
      username: e.detail.value
    })
  },
  passwordInput(e) {
    this.setData({
      password: e.detail.value
    })
  },
  
// 点击登录
  clickLogin: function (e) {
    var that = this;
    if(that.data.username.length==0){
      wx.showToast({
        title: '不能为空',
        icon:'none',
        duration:2000
      })
    }else{
      console.log(that.data.username)
      wx.request({
        url: app.globalData.url +"/wxlogin",
       
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          'username': that.data.username,//"0101",
          'password': that.data.password//"123456"//
        },
        success: function (res) {
          getApp().globalData.userId=that.data.username
          var resData = res.data;  
          console.log(resData.length)
          console.log('aa7824e1-be77-44e7-b38f-48c5b2c4e8d1'.length)
          app.globalData.header.Cookie='JSESSIONID=' + resData;
          wx.setStorageSync('serverSeesion', resData);
          console.log(app.globalData.header.Cookie)
          if (resData.length==36) {         
            wx.showToast({
                title: '登录成功',
                
                duration: 3000
              })
             
              if(that.data.username.length==8){
              wx.navigateTo({
                url: '/pages/menu/menu',
                data:{
                  'username':that.data.username
                }
              })
            }else{
              wx.navigateTo({
                url: '/pages/dormMaster/adminMenu',
                data:{
                  'username':that.data.username
                }
              })
            }
          } else {
            wx.showToast({
              title: '登录失败，用户名密码错误',
              icon:"none",
              duration: 2000
            })
          }
        },
        fail(res){
          wx.showToast({
            title: '请求异常，登录失败',
            icon:"none",
            duration: 2000
          }),
          that.reset();
        }
      })
    }
   
  },
  reset() {
    this.password = '',
      this.username = ''
  }

 
})